/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @Original Author: jin Chen
 * @date: Oct 31, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.dataloader.v1.gui;

import edu.psu.geovista.app.dataloader.v1.components.DataAssembler;
import edu.psu.geovista.app.pcp.toolkits.selectcontrol.SelectItemsPane;
import edu.psu.geovista.app.pcp.toolkits.selectcontrol.ui.VerticalBttnsPane;
import edu.psu.geovista.app.pcp.toolkits.table.ConfigureTable;
import edu.psu.geovista.app.pcp.toolkits.table.ConfigureTableModel;

import java.awt.*;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;

public  class TSDataSelectPane extends DataSelectPane{
        public static final String Col_TsValue="TsValue";

        public TSDataSelectPane(DataAssembler dataAssembler) {
            super(dataAssembler);
        }

        public Properties getTimeValues(){
            Properties timevs=new Properties();
            ConfigureTableModel model=(ConfigureTableModel) this.rightTable.getModel();
            int rowcount=model.getRowCount();
            Object[] names=rightTable.getColumnValues(Col_Name);
            Object[] tvs=rightTable.getColumnValues(Col_TsValue); //treat the time value as text(String)
            for (int i=0;i<rowcount;i++){
                timevs.put(names[i],tvs[i]);
            }
            return timevs;

        }

        public void setTimeValues(Properties tvs){
            int rowcount=rightTable.getRowCount() ;

            for (int i=0;i<rowcount;i++){
               String attn=(String) rightTable.getValueAt(i,Col_Name);//attribute name
               String tv= (String) tvs.get(attn);
               if(tv!=null){
                   rightTable.setValueAt(tv,i,Col_TsValue);
               }
            }

        }


            protected void initButtons() {
                //String[] names=new String[]{Btn_Select,Btn_SelectAll,Btn_Deselect,Btn_DeselectAll/*,"Apply"*/};
                //this.addButtons(names);
                VerticalBttnsPane selectP = createSelectBttnsPane();
          btnP.setLayout(new BorderLayout());
        btnP.add(selectP);

            }
            protected void initRightTable() {
                //1. Model
                int initRowCount=0;
                Vector colName=new Vector();
                colName.add(Col_Name);colName.add(Col_NumRec); colName.add(Col_TsValue);
                Class[] types = new Class [] {
                         java.lang.String.class, java.lang.Integer.class,java.lang.Integer.class
                };
                boolean[] editable={false,false,true};
                ConfigureTableModel model=new ConfigureTableModel(colName,initRowCount);
                model.setTypes(types);
                model.setEditables(editable);
                rightTable.setModel(model);
                this.setColumnsWidth(new int[]{3,1,2}, SelectItemsPane.TABLE_RIGHT);
            }
             protected void selectRecord(String columnName) {
                 int rowCount=0;
                 Number tsValue=null;
                 if(dataAssembler!=null){
                    rowCount=this.dataAssembler.getAttribute(columnName).size(); //see how many rows this column has
                    Map TsValues=dataAssembler.getTimeValues();
                    if(TsValues!=null&&TsValues.size() >0) {
                        //if no tsMeta file found, it is null
                        //tsValue=(Integer) TsValues.get(columnName);
                        tsValue=(Number) TsValues.get(columnName);
                    }
                 }
                 Object[] arow={columnName, new Integer(rowCount),tsValue };
                 this.rightTable.addRow(arow);
             }
             public  void doApply() throws Exception {
                 ConfigureTable table=this.rightTable;
                 boolean allTsValid=true; //if ime series data is valid for all columns
                 for (int i=0;i<table.getRowCount() ;i++){
                     Number tsV=(Number) table.getValueAt(i,Col_TsValue);
                     if (tsV==null)  {
                         allTsValid=false;
                         break;
                     }
                     if(tsV.intValue() <0||tsV.intValue()>1000000){//1000000 is nonsense for time value
                         allTsValid=false;
                         break;
                     }
                 }
                 if(allTsValid){
                     String[] itemNames=this.getSelectedItemNames();//new String[os.length ];
                     if(itemNames==null||itemNames.length <=0){//no data is selected
                        throw new Exception("Please select Time Series data to load.");
                    }

                    //Object[] os= rightTable.getSortValueAtColumn(Col_Name);
                    Object[] TSV= rightTable.getSortValueAtColumn(Col_TsValue);

                    //int[] tsValues=new int[itemNames.length ];
                    Hashtable timeValues=new Hashtable(itemNames.length);
                    for (int i=0;i<itemNames.length ;i++){
                        //itemNames[i]=(String)os[i];
                        //tsValues[i]=((Float)TSV[i]).floatValue()) ;
                        timeValues.put(itemNames[i],TSV[i]);
                    }
                    dataAssembler.setTimeValues(timeValues);
                    dataAssembler.assembleData(itemNames);

                    dataAssembler.cleanData() ;
                    this.initLeftTable();
                 }
                 else{
                     throw new Exception("Please provide Time Series data for all the rows. ");
                     //JOptionPane.showMessageDialog(this,"Please provide Time Series data for all the rows. ");
                 }
             }
        }
